<template>
  <div class="app-container">
    <!-- 条件查询 -->
    <el-form :inline="true" size="mini">
      <el-form-item label="标签名称:">
        <el-input v-model.trim="query.name"></el-input>
      </el-form-item>
      <el-form-item label="分类名称:">
        <!-- clearable 清空按钮，filterable 是否可搜索 -->
        <el-select v-model="query.categoryId" clearable filterable>
          <el-option
            v-for="item in categoryList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" type="primary" @click="queryData"
          >查询</el-button
        >
        <el-button icon="el-icon-refresh" class="filter-item" @click="reload"
          >重置</el-button
        >
        <el-button
          type="primary"
          size="mini"
          icon="el-icon-circle-plus-outline"
          @click="openAdd"
          >新增</el-button
        >
      </el-form-item>
    </el-form>
    <!-- 表格区域 -->
    <el-table :data="list" border highlight-current-row style="width: 100%">
      <!-- type="index"获取索引值，从1开始 ，label显示标题，prop 数据字段名，width列宽 -->
      <el-table-column align="center" type="index" label="序号" width="60">
      </el-table-column>
      <el-table-column
        align="center"
        prop="name"
        label="标签名称"
      ></el-table-column>
      <el-table-column
        align="center"
        prop="categoryName"
        label="分类名称"
      ></el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row.id)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template></el-table-column
      ></el-table
    >
    <!-- 分页 -->
    <pageination
      :currentPage="page.current"
      :pageSizeInfo="page.size"
      :total="page.total"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    />
    <!-- 弹窗 -->
    <edit
      :dialogVisible="edit.dialogVisible"
      :title="edit.title"
      :formData="edit.formData"
      :categoryList="categoryList"
      :remoteForm="remoteForm"
    />
  </div>
</template>

<script>
import { ApiGetLabelList, ApiGetNormalStatus, ApiGetById, ApiDelId } from '@/api/label'
import pageination from '@/components/common/pageination.vue'
import edit from './components/edit.vue'
export default {
  naem: "Label",
  components: {
    pageination,
    edit
  },
  data () {
    return {
      list: [],
      query: {},
      page: {
        total: 0,
        current: 1,
        size: 20
      },
      categoryList: [], // 正常分类数组
      edit: { // 弹窗条件
        dialogVisible: false,
        title: '新增',
        formData: {}
      }
    }
  },
  methods: {
    async fetchData () { //查询标签管理列表
      const { data: res } = await ApiGetLabelList(this.query, this.page.current, this.page.size)
      this.list = res.records
      this.page.total = res.total
    },
    async handleEdit (id) { // 编辑操作
      const res = await ApiGetById(id)
      if (res.code === 20000) {
        this.edit.title = '编辑'
        this.edit.dialogVisible = true
        this.edit.formData = res.data
      }
    },
    handleDelete (id) { // 删除操作
      this.$confirm('确认删除这条记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await ApiDelId(id)
        this.$message({
          type: res.code === 20000 ? 'success' : 'warn',
          message: res.message
        });
        // 刷新列表
        this.fetchData()
      }).catch(() => {

      })
    },
    // 当前条数
    handleSizeChange (val) {
      this.page.size = val
      this.fetchData()
    },
    // 当前页数
    handleCurrentChange (val) {
      this.page.current = val
      this.fetchData()
    },
    async getNomalStatus () { // 查询所有正常分类数据
      const res = await ApiGetNormalStatus()
      this.categoryList = res.data
    },
    queryData () {
      this.page.current = 1
      this.fetchData()
    },
    reload () {
      this.query = {}
      this.fetchData()
    },
    openAdd () { // 打开弹窗
      this.getNomalStatus()
      this.edit.title = '新增'
      this.edit.dialogVisible = true
    },
    remoteForm () { // 关闭弹窗
      this.edit.formData = {}
      this.edit.dialogVisible = false
    }
  },
  created () {
    this.fetchData()
    this.getNomalStatus()
  },
}
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;
  .el-table {
    margin-bottom: 20px;
  }
}
</style>